package com.csx.kotlndemo.viewmodel;

import android.app.Application;
import android.os.Handler;

import com.csx.kotlndemo.MainActivity;

import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

/**
 * MainActivity中的ViewModel操作类
 */
public class MainViewModel extends BaseViewModel {
    public MainViewModel(@NonNull Application application) {
        super(application);
    }

    public void getData(final MainActivity activity){

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                    Handler handler = new Handler(activity.getMainLooper());
                    handler.post(new TimerTask() {
                        @Override
                        public void run() {
                            if(i>20){
                                    activity.refresh("错误");
                            }
                            activity.refresh(i+"");
                        }
                    });
                i++;

            }
        },2000,2000);
    }

    int i=0;
    @Override
    public MutableLiveData<String> getData() {
        Timer timer = new Timer();
        final MutableLiveData<String> data=new MutableLiveData<>();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                i++;

                if(i>20){
                    errLiveData.postValue("错误");
//                    this.cancel();
//                    return;
                }
                data.postValue(i+"");
            }
        },2000,2000);
        return data;
    }

    public MutableLiveData<String> testValue(String str){
        /**可以理解为一个数据暂存对象*/
        MutableLiveData<String> md = new MutableLiveData<>();
        md.setValue(str);
        return md;
    }


}


